package pro.cherish.knowledgescattered.configFile.PropertiesRead;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 创建人：cherish
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2019-11-03 cherish 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：TokenManagerConfig
 * @Description 功能说明：
 * <p>
 * TODO
 * </p>
 * **********************************************************************
 * @date 创建日期： 2019-11-03 cherish
 */

@EnableConfigurationProperties
@Configuration
public class TokenManagerConfig {

    @Autowired
    private TokenProperties properties;

    @Bean
    public TokenManageService tokenManageService() {
        TokenManageService tokenManageService = new TokenManageService();
        String active = this.properties.getActive();
        if ("mapManage".equals(active)) {
            tokenManageService.setActice(active);
            tokenManageService.setTokenManager(userTokenMapManager());
        } else if ("redisManage".equals(active)) {
            tokenManageService.setActice(active);
            tokenManageService.setTokenManager(userTokenRedisManager());
        } else {
            throw new RuntimeException("当前token模式 " + active + " 不支持");
        }
        return tokenManageService;
    }

    @Bean
    public UserTokenMapManager userTokenMapManager() {
        UserTokenMapManager manager = new UserTokenMapManager();
        manager.setTimeout(this.properties.getMapManageTimeout());
        return manager;
    }

    @Bean
    public UserTokenRedisManager userTokenRedisManager() {
        UserTokenRedisManager manager = new UserTokenRedisManager();
        manager.setTimeout(this.properties.getRedisManageTimeout());
        return manager;
    }
}

